Import diddy
Import gameglobals

Class HeroWalkabout Extends Sprite
	Field vx:Float, vy:Float
	Field ax:Float, ay:Float
	Field animating:Bool = false
	Field stopframe:Int = 0
	Field map:HTileMap
'	Field speed:Int = 1
	Field ygo:Int = 0
	Field xgo:Int = 0
	Field doorcol:Bool = False
	Field door:Int = 0
	Method New(gi:GameImage, x#, y#)
		Super.New(gi, x, y)
		rotation = 0
		frame = 3
	End
	
	Method UpdateMovement()
	
		If xgo <> 0 Or ygo <> 0 Then
			'Print "XGO: "+xgo+" YGO: "+ygo
			If xgo > 0 Then 
				xgo = xgo - 1': catx(whoi * 5) -= herow(whoi).speed
				CURRENTMAPX = CURRENTMAPX + 1
			endif
			If xgo < 0 Then 
				xgo = xgo + 1': catx(whoi * 5) += herow(whoi).speed
				CURRENTMAPX = CURRENTMAPX - 1
			endif
			If ygo > 0 Then 
				ygo = ygo - 1': caty(whoi * 5) -= herow(whoi).speed
				CURRENTMAPY = CURRENTMAPY + 1
			Endif
			If ygo < 0 Then 
				ygo = ygo + 1': caty(whoi * 5) += herow(whoi).speed	
				CURRENTMAPY = CURRENTMAPY - 1	
			Endif
		Endif
		
		
			
	End

	
	Method Update:Void()
		Local Acceleration:Float
		
		'SetFrame:Void(startFrame:Int=0, endFrame:Int=0, speed:Int=125, pingPong:Bool = False, loop:Bool = True)
		If KeyDown(KEY_UP)
			If map<>Null Then
				If map.TileCollision("collision_0", x, y-1, image.w, image.h, CURRENTMAPX, CURRENTMAPY) = 0
					If xgo = 0 And ygo = 0 Then
						ygo = -map.tileHeight
						'Print "YGO: "+ygo
						If stopframe <> 0 Or animating = false Then
							SetFrame(0,1,50,False,True)
							animating = true
						endif						
						stopframe = 0
						
					endif
					'CURRENTMAPY-= 1'speed+=.3

				End If
				
				'check doors
				If map.DoorCollision(x, y-1,image.w,image.h, CURRENTMAPX, CURRENTMAPY+map.tileHeight-1) = 1 Then
					
					If map.entrydoorcol = False
						Print "Door Collision Map: "+CURRENTMAP
						Local DL:Int = map.GetDoorLink(map.currentdoorcol)
						If DL <> -1 Then
							
							'Get new X,Y,Map
							'haltcontrol = True
							xgo = 0
							ygo = 0
							doorcol = True
							door = DL
							'loadScreen = New LoadingScreen(Tilemaps[CURRENTMAP].links[DL].dest_map,Tilemaps[CURRENTMAP].links[DL].dest)
							'FadeToScreen(loadScreen)'diddyGame.exitScreen)				
						
						Endif
					endif
				Else
					map.entrydoorcol = false
				Endif				
				
			End If
			
		Else If KeyDown(KEY_DOWN)
			If map<>Null Then
				If map.TileCollision("collision_0", x, y+1, image.w, image.h, CURRENTMAPX, CURRENTMAPY+map.tileHeight) = 0		
					If xgo = 0 And ygo = 0 Then
						ygo = map.tileHeight
						'Print "YGO: "+ygo
						If stopframe <> 4 Or animating = false then
							SetFrame(4,5,50,False,True)
							animating = true
							
						endif						
						stopframe = 4
					endif
					
					'CURRENTMAPY+= 1'speed-=.3

				End If
				
				If map.DoorCollision(x, y+1,image.w,image.h, CURRENTMAPX, CURRENTMAPY-map.tileHeight+1) = 1 Then
					
					If map.entrydoorcol = False
						Print "Door Collision Map: "+CURRENTMAP
						Local DL:Int = map.GetDoorLink(map.currentdoorcol)
						If DL <> -1 Then
							
							'Get new X,Y,Map
							'haltcontrol = True
							xgo = 0
							ygo = 0
							doorcol = True
							door = DL
							'loadScreen = New LoadingScreen(Tilemaps[CURRENTMAP].links[DL].dest_map,Tilemaps[CURRENTMAP].links[DL].dest)
							'FadeToScreen(loadScreen)'diddyGame.exitScreen)				
						
						Endif
					endif
				Else
					map.entrydoorcol = false
				Endif				
				
			End If
			
				
		Else If KeyDown(KEY_LEFT)
			If map<>Null Then
				If map.TileCollision("collision_0", x-1, y, image.w, image.h, CURRENTMAPX, CURRENTMAPY) = 0			
					If xgo = 0 And ygo = 0 Then
						xgo = -map.tileWidth
						'Print "XGO: "+xgo
						If stopframe <> 6 Or animating = false then
							SetFrame(6,7,50,False,True)
							animating = true
						endif						
						stopframe = 6
					endif
					'CURRENTMAPX-= 1'Self.rotation+=2

				End If
				
				If map.DoorCollision(x-1, y,image.w,image.h, CURRENTMAPX+map.tileWidth, CURRENTMAPY+1) = 1 Then
					
					If map.entrydoorcol = False
						Print "Door Collision Map: "+CURRENTMAP
						Local DL:Int = map.GetDoorLink(map.currentdoorcol)
						If DL <> -1 Then
							
							'Get new X,Y,Map
							'haltcontrol = True
							xgo = 0
							ygo = 0
							doorcol = True
							door = DL
							'loadScreen = New LoadingScreen(Tilemaps[CURRENTMAP].links[DL].dest_map,Tilemaps[CURRENTMAP].links[DL].dest)
							'FadeToScreen(loadScreen)'diddyGame.exitScreen)				
						
						Endif
					endif
				Else
					map.entrydoorcol = false
				Endif				
				
			End If
			
		
		Else If KeyDown(KEY_RIGHT)
			If map<>Null Then
				If map.TileCollision("collision_0", x+1, y, image.w, image.h, CURRENTMAPX+map.tileWidth, CURRENTMAPY) = 0				
					If xgo = 0 And ygo = 0 Then
						xgo = map.tileWidth
						'Print "XGO: "+xgo
						If stopframe <> 2 Or animating = false then
							SetFrame(2,3,50,False,True)
							animating = true
						endif						
						stopframe = 2
					endif
					'CURRENTMAPX+= 1'Self.rotation-=2

				End If
				
				If map.DoorCollision(x+1, y,image.w,image.h, CURRENTMAPX-map.tileWidth, CURRENTMAPY+1) = 1 Then
					
					If map.entrydoorcol = False
						Print "Door Collision Map: "+CURRENTMAP
						Local DL:Int = map.GetDoorLink(map.currentdoorcol)
						If DL <> -1 Then
							
							'Get new X,Y,Map
							'haltcontrol = True
							xgo = 0
							ygo = 0
							doorcol = True
							door = DL
							'loadScreen = New LoadingScreen(Tilemaps[CURRENTMAP].links[DL].dest_map,Tilemaps[CURRENTMAP].links[DL].dest)
							'FadeToScreen(loadScreen)'diddyGame.exitScreen)				
						
						Endif
					endif
				Else
					map.entrydoorcol = false
				Endif				
				
			End If
			
		End If
		
		If Not KeyDown(KEY_UP) And Not KeyDown(KEY_DOWN) And Not KeyDown(KEY_LEFT) And Not KeyDown(KEY_RIGHT) And xgo = 0 And ygo = 0 Then
			SetFrame(stopframe,stopframe,50,False,True)
			animating = false
		endif
		UpdateMovement()
		'MoveForward()
		
		If x < 0 x = SCREEN_WIDTH
		If x > SCREEN_WIDTH x = 0
		If y < 0 y = SCREEN_HEIGHT
		If y > SCREEN_HEIGHT y = 0
	End
	
End